﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class ReportDetail : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.ReportParameter> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.ReportParameter> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static string GetReportByReportID(string reportID)
        {
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReportByReportID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), reportID);
            if (dsPromotion != null)
            {
                return JsonConvert.SerializeObject(dsPromotion);
            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetReportParameter(string reportID)
        {
            if (HttpContext.Current.Session["ReportParameterList"] != null)
            {
                List<Model.ReportParameter> lstDocLineUpdate = (List<Model.ReportParameter>)HttpContext.Current.Session["ReportParameterList"];
                List<Model.ReportParameter> filteredResults = lstDocLineUpdate.FindAll(p => p.LineStatus != "3");
                HttpContext.Current.Session["ReportParameterList"] = filteredResults;
                HttpContext.Current.Session["ReportParameterCount"] = filteredResults.Count;
                return new Response(filteredResults, filteredResults.Count);
            }
            List<Model.ReportParameter> lstDocLine = new List<Model.ReportParameter>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReportParameterByReportID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), reportID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.ReportParameter reportParameterLine = new Model.ReportParameter();
                    reportParameterLine.ReportDetailID = r["ReportDetailID"].ToString();
                    reportParameterLine.ReportID = r["ReportID"].ToString();
                    reportParameterLine.ParaName = r["ParaName"].ToString();
                    reportParameterLine.ParaCaption = r["ParaCaption"].ToString();
                    reportParameterLine.ParaType = r["ParaType"].ToString();
                    reportParameterLine.ReferenceName = r["ReferenceName"].ToString();
                    reportParameterLine.ParaFromQuery = r["ParaFromQuery"].ToString();
                    reportParameterLine.LineStatus = "2";
                    lstDocLine.Add(reportParameterLine);
                }
                HttpContext.Current.Session["ReportParameterList"] = lstDocLine;
                HttpContext.Current.Session["ReportParameterCount"] = dsPromotion.Tables[0].Rows.Count;
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocLine };
                return new Response(lstDocLine, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateReport(string oReportParameterLine, string isInsert)
        {
            Model.ReportParameter line = JsonConvert.DeserializeObject<Model.ReportParameter>(oReportParameterLine);
            if (HttpContext.Current.Session["ReportParameterList"] != null)
            {
                List<Model.ReportParameter> lstDocLine = (List<Model.ReportParameter>)HttpContext.Current.Session["ReportParameterList"];
                var result = (from m in lstDocLine
                              select m).OrderByDescending(a => a.ReportDetailID).FirstOrDefault(); ;
                if (isInsert == "1")
                {
                    if (result != null)
                    {
                        line.ReportDetailID = (int.Parse(result.ReportDetailID) + 1).ToString();
                    }
                    else
                    {
                        line.ReportDetailID = "1";
                    }
                    line.LineStatus = "1";
                    lstDocLine.Add(line);
                }
                else if (isInsert == "2")
                {
                    line.LineStatus = "2";

                    Model.ReportParameter updateLine = lstDocLine.FirstOrDefault(u => u.ReportDetailID == line.ReportDetailID);
                    int index = lstDocLine.FindIndex(u => u.ReportDetailID == line.ReportDetailID);
                    updateLine.ParaName = line.ParaName;
                    updateLine.ParaCaption = line.ParaCaption;
                    updateLine.ParaType = line.ParaType;
                    updateLine.ReferenceName = line.ReferenceName;
                    updateLine.ParaFromQuery = line.ParaFromQuery;
                }
                HttpContext.Current.Session["ReportParameterList"] = lstDocLine;
                HttpContext.Current.Session["ReportParameterCount"] = lstDocLine.Count;
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleleReportParameter(string reportDetailID)
        {
            if (HttpContext.Current.Session["ReportParameterList"] != null)
            {
                List<Model.ReportParameter> lstDocLine = (List<Model.ReportParameter>)HttpContext.Current.Session["ReportParameterList"];
                Model.ReportParameter updateLine = lstDocLine.FirstOrDefault(u => u.ReportDetailID == reportDetailID);
                if (updateLine.ReportID.Length > 0)
                {
                    updateLine.LineStatus = "3";
                }
                else
                {
                    lstDocLine.Remove(updateLine);
                }
                HttpContext.Current.Session["ReportParameterList"] = lstDocLine;
                HttpContext.Current.Session["ReportParameterCount"] = lstDocLine.Count;
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static string UpdateReportToDB(string oReportHeader, bool isUpdate)
        {
            Model.Report header = JsonConvert.DeserializeObject<Model.Report>(oReportHeader);
            List<Model.ReportParameter> lstDocLine = new List<Model.ReportParameter>();
            if (HttpContext.Current.Session["ReportParameterList"] != null)
            {
                lstDocLine = (List<Model.ReportParameter>)HttpContext.Current.Session["ReportParameterList"];
            }
            if (header != null)
            {
                if (HttpContext.Current.Session["LoginUser"] != null)
                {
                    DataTable tbUser = (DataTable)HttpContext.Current.Session["LoginUser"];

                    if (tbUser != null && tbUser.Rows.Count > 0)
                    {
                        header.CreatedUserID = tbUser.Rows[0]["UserID"].ToString();
                        header.CompanyCode = tbUser.Rows[0]["CompanyCode"].ToString();
                    }
                }
                header.ReportType = "3"; // User Defined
                header.ReportURL = ConfigurationManager.AppSettings["USER_DEFINED_REPORT_PAGE"].ToString();
                Promotion pmt = new Promotion();
                string headerJson = JsonConvert.SerializeObject(header);
                string lineJson = JsonConvert.SerializeObject(lstDocLine);
                string errMsg = pmt.CreateReport(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), header.ReportID, isUpdate, headerJson, lineJson);
                return errMsg;
            }
            return "0";
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void ClearSessionData()
        {
            HttpContext.Current.Session["ReportParameterList"] = null;
            HttpContext.Current.Session["ReportParameterCount"] = null;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllReferenceValue(string referenceCode)
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), referenceCode);
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);

            }
            return string.Empty;
        }
    }
}
